#include <irrlicht.h>

using namespace irr;

#include "CPLYMeshFileLoader.h"

#include <iostream>

int main(int argc, char** argv)
{
	if (!(argc > 1))
	{
		std::cout << "Usage: plyloader [ply file]" << std::endl;
		return 1;
	}

	IrrlichtDevice* device = createDevice(video::EDT_OPENGL,
										  core::dimension2d<s32>(640, 480),
										  16,
										  false);
	if (!device)
	{
		std::cout << "Unable to create OpenGL device. Quitting..." << std::endl;
		return 1;
	}

	device->getSceneManager()->addExternalMeshLoader
		(new scene::CPLYMeshFileLoader(device->getSceneManager()->getMeshManipulator(), device->getLogger()));

	scene::IAnimatedMesh* mesh = device->getSceneManager()->getMesh(argv[1]);

	if (!mesh)
	{
		std::cout << "Unable to load mesh: " << argv[1] << std::endl;
		return 1;
	}

	scene::ISceneNode* node =
		device->getSceneManager()->addAnimatedMeshSceneNode(mesh);

	if (!node)
	{
		std::cout << "Unable to create node with mesh: " << argv[1] << std::endl;
		return 1;
	}

	node->setMaterialFlag(video::EMF_LIGHTING, false);
	node->setMaterialTexture(0, device->getVideoDriver()->getTexture("uv.jpg"));
	node->setScale(core::vector3df(20,20,20));

	scene::ICameraSceneNode* cam =
		device->getSceneManager()->addCameraSceneNodeMaya();

	if (!cam)
	{
		std::cout << "Unable to create camera. Who knows why." << std::endl;
		return 1;
	}

	while (device->run())
	{
		device->getVideoDriver()->beginScene(true, true, video::SColor(0,0,0,0));
		device->getSceneManager()->drawAll();
		device->getVideoDriver()->endScene();
	}
	device->drop();

	return 0;
}

